﻿using System.Collections.Generic;
using Engine;
using Engine.Graphics;
using Game;
namespace Mekiasm.Common
{
    public class ModelItem
    {
        public Texture2D Texture2D;
        public BlockMesh BlockMesh = new BlockMesh();
        public BlockMesh TransformedBlockMesh = new BlockMesh();
        public string meshName;
        public string textureName;
        public Texture2D RenderTexture;
        public List<BlockMesh> Frames = new List<BlockMesh>();

        public ModelItem(string meshName, string textureName)
        {
            this.meshName = meshName;
            this.textureName = textureName;
        }
        public void Load(Model model, Texture2D texture2D, Matrix? transform = null)
        {
            Texture2D = texture2D;
            Matrix matrix = BlockMesh.GetBoneAbsoluteTransform(model.FindMesh(meshName).ParentBone);
            if (transform.HasValue) matrix *= transform.Value;
            BlockMesh.AppendModelMeshPart(model.FindMesh(meshName).MeshParts[0], matrix, false, false, false, true, Color.White);
            Transform(model, Matrix.Identity);
        }
        public void Transform(Model model, Matrix matrix)
        {
            TransformedBlockMesh.Indices.Clear();
            TransformedBlockMesh.Vertices.Clear();
            Matrix matrix2 = BlockMesh.GetBoneAbsoluteTransform(model.FindMesh(meshName).ParentBone);
            matrix *= matrix2;
            TransformedBlockMesh.AppendModelMeshPart(model.FindMesh(meshName).MeshParts[0], matrix, false, false, false, true, Color.White);
        }
    }

}
